package 课设;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class blogAdd extends JFrame {

    private JTextField title;
    private JTextArea content;
    private JButton save;

    public blogAdd(String username) {

        this.setSize(600,800);
        this.setTitle("添加博客");
        this.setLocationRelativeTo(null);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        this.getContentPane().add(splitPane);
        splitPane.add(title = new JTextField("标题",1));

        splitPane.setDividerLocation(50);
        splitPane.setOneTouchExpandable(true);

        //BorderLayout a = new BorderLayout();
        JPanel x = new JPanel();
        splitPane.add(x);

        //content = new JTextArea("正文",10000,50);
        //JScrollPane contentScrollPane = new JScrollPane(content);
        //x.add(contentScrollPane);

        content = new JTextArea(40, 50);
        content.setWrapStyleWord(true);
        content.setLineWrap(true);

        JScrollPane msgScroller = new JScrollPane();
        msgScroller.setBorder(
                BorderFactory.createTitledBorder("正文"));
        msgScroller.setViewportView(content);

        x.add(msgScroller);

        save = new JButton("保存");
        this.add(save, BorderLayout.SOUTH);
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                db dbcon = new db();
                try {
                    String sql1 = "insert into blog values(?,?,?,getdate(),?)";//id,title,summary,post,userId
                    PreparedStatement prestate = null;  //PreparedStatement __________ = dbcon.PreparedStatement(sql)

                    ResultSet set = dbcon.executeQuery("select top 1 * from blog order by blogId desc");
                    int blogId = 1;
                    if(set.next())
                        blogId = set.getInt("blogId") + 1;


                    prestate = dbcon.PreparedStatement(sql1);
                    prestate.setInt(1, blogId);
                    prestate.setString(2, title.getText());

                    String s = content.getText();
                    if(s.length() > 50) s = s.substring(0,50)+"...";
                    prestate.setString(3, s);
                    ResultSet set1 = dbcon.executeQuery("select * from users where username = '" + username + "'");
                    int id = -1;
                    if(set1.next())
                        id = set1.getInt("userId");
                    prestate.setInt(4, id);
                    prestate.executeUpdate();

                    String sql2 = "insert into detailed_blog values(?,?,?)";//article, content, blogId
                    PreparedStatement preparedStatement = dbcon.PreparedStatement(sql2);
                    preparedStatement.setString(1, title.getText());
                    preparedStatement.setString(2, content.getText());
                    preparedStatement.setInt(3, blogId);
                    preparedStatement.executeUpdate();

                    dispose();
                    blog_show psel = new blog_show(username);
                    psel.setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
    public blogAdd(int userId) {

        this.setSize(600,800);
        this.setTitle("添加博客");
        this.setLocationRelativeTo(null);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        this.getContentPane().add(splitPane);
        splitPane.add(title = new JTextField("标题",1));

        splitPane.setDividerLocation(50);
        splitPane.setOneTouchExpandable(true);

        //BorderLayout a = new BorderLayout();
        JPanel x = new JPanel();
        splitPane.add(x);

        //content = new JTextArea("正文",10000,50);
        //JScrollPane contentScrollPane = new JScrollPane(content);
        //x.add(contentScrollPane);

        content = new JTextArea(40, 50);
        content.setWrapStyleWord(true);
        content.setLineWrap(true);

        JScrollPane msgScroller = new JScrollPane();
        msgScroller.setBorder(
                BorderFactory.createTitledBorder("正文"));
        msgScroller.setViewportView(content);

        x.add(msgScroller);

        save = new JButton("保存");
        this.add(save, BorderLayout.SOUTH);
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                db dbcon = new db();
                try {
                    String sql1 = "insert into blog values(?,?,?,getdate(),?)";//id,title,summary,post,userId
                    PreparedStatement prestate = null;  //PreparedStatement __________ = dbcon.PreparedStatement(sql)

                    ResultSet set = dbcon.executeQuery("select top 1 * from blog order by blogId desc");
                    int blogId = 1;
                    if(set.next())
                        blogId = set.getInt("blogId") + 1;


                    prestate = dbcon.PreparedStatement(sql1);
                    prestate.setInt(1, blogId);
                    prestate.setString(2, title.getText());

                    String s = content.getText();
                    if(s.length() > 50) s = s.substring(0,50)+"...";
                    prestate.setString(3, s);

                    prestate.setInt(4, userId);
                    prestate.executeUpdate();

                    String sql2 = "insert into detailed_blog values(?,?,?)";//article, content, blogId
                    PreparedStatement preparedStatement = dbcon.PreparedStatement(sql2);
                    preparedStatement.setString(1, title.getText());
                    preparedStatement.setString(2, content.getText());
                    preparedStatement.setInt(3, blogId);
                    preparedStatement.executeUpdate();

                    dispose();
                    user_show psel = new user_show(userId);
                    psel.setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] agrs) {
        blogAdd w = new blogAdd("张三");
        w.setVisible(true);
    }
}

